<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <span style="color:red;font-size:18px;font-weight:900;">
                温馨提示：更新后需要按Ctrl+F5强制刷新页面后再使用
            </span>
        </el-card>
        <el-card class="!border-none mt-4 supgrade" shadow="never">
            <el-descriptions title="当前版本" :column="1" :size="size" :style="blockMargin">
                <el-descriptions-item label="版本号">{{
                    currentVersion
                }}</el-descriptions-item>
                <el-descriptions-item label="发布时间">{{
                    vurrentUpdate
                }}</el-descriptions-item>
            </el-descriptions>
            <el-descriptions title="检查新版" :column="1" :size="size" :style="blockMargin">
                <el-descriptions-item label="">
                    <el-button @click="getVersion" type="primary" :loading="btnFlah" :disabled="updateDisabled">{{
                        toUpdateFlag ? "开始更新-类库" : "检查版本"
                    }}</el-button></el-descriptions-item>
            </el-descriptions>


            <el-descriptions v-show="updateFlag" :column="1" :size="size" :style="blockMargin"
                v-for="(item, index) in verListItem" :key="index" :title="item.version_no">
                <el-descriptions-item label="版本名称">
                    {{ item.version_name }}
                </el-descriptions-item>
                <el-descriptions-item label="版本描述">
                    {{ item.version_description }}
                </el-descriptions-item>
                <el-descriptions-item label="发布于">
                    {{ item.create_time }}
                </el-descriptions-item>
                <!-- <el-descriptions-item v-for="(citem, cindex) in item.version_description" :key="cindex">
                    <el-tag size="small" :type="VersionTagsEnum[citem.descType]">{{ UpdateVersionTagsEnumMap[citem.descType]
                    }}</el-tag>
                    &nbsp;{{ citem.descValue }}
                </el-descriptions-item> -->
                <el-descriptions-item>
                    <el-divider />
                </el-descriptions-item>
            </el-descriptions>
        </el-card>
    </div>
</template>

<script setup lang="ts">
import {
    getClassCurrentVersion,
    getClassVersion,
    updateClassStep,
} from "@/api/cloudupdate";
const toUpdateFlag = ref<boolean>(false);
const size = ref("");
const blockMargin = computed(() => {
    const marginMap = {
        large: "32px",
        default: "28px",
        small: "24px",
    };
    return {
        marginTop: marginMap[size.value] || marginMap.default,
    };
});
const stepsArr = ref([
    {
        title: "下载类库版本",
        status: 0,
        msg: "",
        statusMsg: "",
    },
    {
        title: "解压文件",
        status: 0,
        msg: "",
        statusMsg: "",
    },
    {
        title: "更新文件",
        status: 0,
        msg: "",
        statusMsg: "",
    },
]);
const currentVersion = ref<string>();
const vurrentUpdate = ref<string>();
const updateFlag = ref<boolean>(false);
const verListItem = ref<any[]>([
    // { descType: 1, descValue: "fdsfsdfsd" },
    // { descType: 1, descValue: "fdsfsdfsd" },
]);
const updateDisabled = ref(false);
const btnFlah = ref<boolean>(false);
const getpsStatusDesc = ["", "更新中", "更新失败", "成功"];
const currstep = ref(0);
const updateFn = (loading: any) => {
    if (currstep.value >= 0 && currstep.value <= 2) {
        stepsArr.value[currstep.value].status = 1;
        stepsArr.value[currstep.value].statusMsg = getpsStatusDesc[1];
        updateClassStep({
            app: "adminapi",
            currstep: currstep.value,
        })
            .then((res) => {
                stepsArr.value[currstep.value].status = 3;
                stepsArr.value[currstep.value].statusMsg = getpsStatusDesc[3];
                console.log("updateFn currstep.value", currstep.value);
                currstep.value++;
                updateFn(loading);
            })
            .catch((ret) => {
                stepsArr.value[currstep.value].msg = ret.msg;
                stepsArr.value[currstep.value].status = 2;
                stepsArr.value[currstep.value].statusMsg = getpsStatusDesc[2];
                console.log("updateFn ret", ret);
                loading.close();
                ElMessageBox.alert(ret.msg, "更新结果", {
                    type: "error",
                    confirmButtonText: "确认",
                });
            });
    }
    if (currstep.value >= 3) {
        updateDisabled.value = true;
        console.log("--更新完成");
        loading.close();
        ElMessageBox.alert("更新成功:" + verListItem.value[0].version_no, "更新结果", {
            type: "success",
            confirmButtonText: "确认",
        });
    }
};
const getVersion = async () => {
    if (toUpdateFlag.value) {
        // console.log("开始更新");
        const loading = ElLoading.service({
            lock: true,
            text: "更新中...",
            background: "rgba(0, 0, 0, 0.7)",
        });
        updateFn(loading);
        return false;
        await updateClassStep({
            app: "adminapi",
        })
            .then((res) => {
                toUpdateFlag.value = false;
                initGetCUrrentVersion();
                loading.close();
                ElMessage({
                    message: "更新成功",
                    type: "success",
                });
            })
            .catch((ret) => {
                loading.close();
                ElMessage({
                    message: "更新失败",
                    type: "error",
                });
            });
        return;
    }
    btnFlah.value = true;
    await getClassVersion({
        app: "adminapi",
    })
        .then((res) => {
            btnFlah.value = false;
            if (res.data.packageList.length == 0) {
                ElMessage({
                    message: "当前版本已是最新版",
                    type: "success",
                });
            } else {
                toUpdateFlag.value = true;
                verListItem.value = res.data.packageList;
                updateFlag.value = true;
            }
        })
        .catch((ret) => {
            btnFlah.value = false;
        });
};

onMounted(async () => {
    initGetCUrrentVersion();
});

const initGetCUrrentVersion = async () => {
    await getClassCurrentVersion({})
        .then((res) => {
            currentVersion.value = res.version;
            vurrentUpdate.value = res.update_time;
        })
        .catch((ret) => { });
    nextTick(() => {
        // handleExpand()
    });
};
</script>